package com.yvan.onekeylock;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

public class CommandActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 状态栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        int commandCode = getIntent().getIntExtra(CommandItem.COMMAND, -1);
        execCommand(commandCode);
    }

    private void execCommand(int commandCode) {
        switch (commandCode) {
            case CommandItem.REBOOT_CODE:
                checkRoot();
                CommandManager.execRootCommand("reboot");
                break;
            case CommandItem.SHUTDOWN_CODE:
                checkRoot();
                CommandManager.execRootCommand("shutdown");
                break;
        }
    }

    private boolean checkRoot() {
        boolean isRoot = CommandManager.rootCommand();
        if (!isRoot) {
            Toast.makeText(this, "未能获取root权限，无法执行指令", Toast.LENGTH_SHORT).show();
            finish();
        }
        return isRoot;
    }
}
